package org.commons;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

public class Utils {


	/**
	 * La funzione hash() si occupa dell'esecuzione dell'algoritmo crittografico MD5
	 * che prende in input una stringa di lunghezza arbitraria
	 *  
	 * @param str 
	 * @return int
	 */
	public static int hash(String str) {
		int result = -1;
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException nsae) {
			System.out.println("No Such Algorithm Exception");
			nsae.printStackTrace();
		}
		result = byteArrayToInt(md.digest(str.getBytes()), 0);
		return result;
	}

	private static int byteArrayToInt(byte[] b, int offset) {
		int value = 0;
		for (int i = 0; i < 4; i++) {
			int shift = (4 - 1 - i) * 8;
			value += (b[i + offset] & 0x000000FF) << shift;
		}
		return value;
	}

	/**
	 * Ritorna un elemento random dell'arraylist fornito in argomento
	 * @param array
	 * @return un elemento random dell'arraylist
	 */
	public static Integer randomFromArrayList(ArrayList<Integer> array) {
		return array.get(Double.valueOf(Math.random()*array.size()).intValue());
	}

	/**
	 * Inserisce il decoy link all'interno delle pagina specificata .
	 * @param decoyLink url del decoy link
	 * @param pagePath assoluto della pagina da modificare
	 */
	static public void addDecoyLinks(String decoyLink, String pagePath, int howMany) {
		StringBuilder contents = new StringBuilder();
		BufferedReader input;
		try {
			input = new BufferedReader(new FileReader(new File(pagePath)));
			try {
				String line = null;
				while ((line = input.readLine()) != null) {
					contents.append(line);
				}
			} finally {
				input.close();
			}
		} catch (IOException e) {
		}
		Writer output = null;
		try {
			for(int i = 0; i < howMany; i++) {
				contents = new StringBuilder(contents.toString().replace("</body>", "<a href=\"" + decoyLink + "\">Decoy link here!</a></body>"));
			}
			output = new BufferedWriter(new FileWriter(new File(pagePath)));
			output.write(contents.toString());
		} catch (IOException e) {
		} finally {
			try {
				output.close();
			} catch (IOException e) {
			}
		}
	}
	
	/**
	 * Rimuove tutti i decoy link dalla pagina specificata.
	 * @param decoyLink url del decoy link
	 * @param pagePath assoluto della pagina da modificare
	 */
	static public void removeAllDecoyLinks(String decoyLink, String pagePath) {
		StringBuilder contents = new StringBuilder();
		BufferedReader input;
		try {
			input = new BufferedReader(new FileReader(new File(pagePath)));
			try {
				String line = null;
				while ((line = input.readLine()) != null) {
					contents.append(line);
				}
			} finally {
				input.close();
			}
		} catch (IOException e) {
		}
		Writer output = null;
		try {
				contents = new StringBuilder(contents.toString().replace("<a href=\"" + decoyLink + "\">Decoy link here!</a>", ""));
			output = new BufferedWriter(new FileWriter(new File(pagePath)));
			output.write(contents.toString());
		} catch (IOException e) {
		} finally {
			try {
				output.close();
			} catch (IOException e) {
			}
		}
	}
}
